package com.app;

import com.app.common.StringUtil;
import com.app.markdown.handler.ListenerHandler;
import com.app.markdown.store.FileStore;
import com.app.markdown.util.FileUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;


public class TestApp2Controller {
    @FXML
    private VBox rootVBox;
    @FXML
    private ListView<String> fileListView;
    @FXML
    private WebView fileContentArea;
    @FXML
    private void initialize() {

        // 注册一个监听器来监听 sceneProperty 的变化
        fileListView.sceneProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                // ListView 现在已添加到场景中
                //openDirectory();
                // 添加键盘事件监听器
                ListenerHandler listenerHandler = new ListenerHandler();
                listenerHandler.addKeyboardListener(rootVBox);

                // 显示文件列表（根据默认路径）
                if(!StringUtil.isEmpty(FileUtil.getDefaultDirectory())){
                    FileUtil.displayFilesInDirectory(fileListView, FileUtil.getDefaultDirectory(),fileContentArea);
                }
            }
        });
    }

    @FXML
    private void handleNew(ActionEvent event) {
        System.out.println("New operation triggered.");
    }

    /**
     * 菜单：open..
     * 在左侧列表，打开目录
     * @param event
     */
    @FXML
    private void handleOpen(ActionEvent event) {
        // 延迟初始化 DirectoryChooser
        if (fileListView.getScene() != null) {
            FileUtil.openDirectory(fileListView, fileContentArea);
        } else {
            // 注册一个监听器来监听 sceneProperty 的变化
            System.out.println("注册一个监听器来监听 sceneProperty 的变化");
            fileListView.sceneProperty().addListener((observable, oldValue, newValue) -> {
                if (newValue != null) {
                    // ListView 现在已添加到场景中
                    System.out.println("ListView 现在已添加到场景中");
                    FileUtil.openDirectory(fileListView, fileContentArea);
                }
            });
        }
    }

    @FXML
    private void handleClose(ActionEvent event) {
        System.out.println("Close operation triggered.");
    }

    @FXML
    private void handleSave(ActionEvent event) {
        System.out.println("Save operation triggered.");
        FileUtil.saveFileContent(FileStore.currentFilePathName);
    }



    @FXML
    private void handleSaveAs(ActionEvent event) {
        System.out.println("Save As operation triggered.");
    }

    @FXML
    private void handleRevert(ActionEvent event) {
        System.out.println("Revert operation triggered.");
    }

    @FXML
    private void handlePreferences(ActionEvent event) {
        System.out.println("Preferences operation triggered.");
    }

    @FXML
    private void handleQuit(ActionEvent event) {
        System.out.println("Quit operation triggered.");
    }

    @FXML
    private void handleUndo(ActionEvent event) {
        System.out.println("Undo operation triggered.");
    }

    @FXML
    private void handleRedo(ActionEvent event) {
        System.out.println("Redo operation triggered.");
    }

    @FXML
    private void handleCut(ActionEvent event) {
        System.out.println("Cut operation triggered.");
    }

    @FXML
    private void handleCopy(ActionEvent event) {
        System.out.println("Copy operation triggered.");
    }

    @FXML
    private void handlePaste(ActionEvent event) {
        System.out.println("Paste operation triggered.");
    }

    @FXML
    private void handleDelete(ActionEvent event) {
        System.out.println("Delete operation triggered.");
    }

    @FXML
    private void handleSelectAll(ActionEvent event) {
        System.out.println("Select All operation triggered.");
    }

    @FXML
    private void handleUnselectAll(ActionEvent event) {
        System.out.println("Unselect All operation triggered.");
    }

    @FXML
    private void handleAbout(ActionEvent event) {
        System.out.println("About operation triggered.");
    }


}